﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using ABCat.UI.WPF.Models;

namespace ABCat.UI.WPF.UI
{
	public partial class GroupsTreeUc : UserControl
	{
		//#region + Events +

		//public event EventHandler<GroupChangedEventArgs> GroupChanged;

		//#endregion
		#region + Fields +

		//private CancellationTokenSource _cancellationTokenSource;
		//private Context _context;
		//private GrouppingLogicModel _viewModel;

		#endregion
		#region + Properties +

		//public Group CurrentGroup
		//{
		//	get { return (Group)GroupsTree.SelectedItem; }
		//}

		//public GrouppingLogicPluginBase CurrentGrouppingLogic
		//{
		//	get { return (GrouppingLogicPluginBase)GrouppingLogicCmb.SelectedItem; }
		//}

		#endregion
		#region + Ctor +

		public GroupsTreeUc()
		{
			InitializeComponent();
		}

		public GrouppingLogicViewModel ViewModel
		{
			get { return (GrouppingLogicViewModel)DataContext; }
			set { DataContext = (GrouppingLogicViewModel)value; }
		}

		#endregion
		#region + Logic +

		//public void Init(Context context)
		//{
		//	_context = context;
		//	//InitGrouppingLogics();
		//}

		//public void RefreshList()
		//{
		//	var groupChanged = GroupChanged;
		//	if (groupChanged != null)
		//	{
		//		groupChanged(this, new GroupChangedEventArgs(CurrentGroup));
		//	}
		//}

		//private void GenerateGroupsAsyncCompleted(Group group)
		//{
		//	if (group == null) return;
		//	if (Dispatcher.CheckAccess())
		//	{
		//		GroupsTree.ItemsSource = new[] { group };
		//		group.IsSelected = true;
		//		group.IsExpanded = true;
		//	}
		//	else
		//	{
		//		Dispatcher.Invoke(() => GenerateGroupsAsyncCompleted(group));
		//	}
		//}

		//private void GrouppingLogicCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
		//{
		//	GrouppingLogicPluginBase grouppingLogic = null;
		//	if (e.AddedItems.Count == 1)
		//	{
		//		var cancellationTokenSource = _cancellationTokenSource;
		//		if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
		//		cancellationTokenSource = new CancellationTokenSource();
		//		_cancellationTokenSource = cancellationTokenSource;
		//		grouppingLogic = (GrouppingLogicPluginBase)e.AddedItems[0];
		//		grouppingLogic.BeginGenerateGroupsAsync(GenerateGroupsAsyncCompleted, cancellationTokenSource.Token);
		//	}
		//	else
		//	{
		//		GroupsTree.ItemsSource = null;
		//	}
		//	DataContext = grouppingLogic;
		//}


		//private void InitGrouppingLogics()
		//{
		//	foreach (var pluginCreatorAttribute in _context.PluginsFactory.GetPlugins<IGrouppingLogicPlugin>())
		//	{
		//		var logic = pluginCreatorAttribute.CreateInstance<IGrouppingLogicPlugin>(_context);
		//		GrouppingLogicCmb.Items.Add(logic);
		//	}
		//	GrouppingLogicCmb.SelectedIndex = 0;
		//}

		//private void On_GroupTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
		//{
		//	var groupChanged = GroupChanged;
		//	if (groupChanged != null)
		//	{
		//		groupChanged(this, new GroupChangedEventArgs(CurrentGroup));
		//	}
		//}

		protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
		{
			base.OnKeyDown(e);

			switch (e.Key)
			{
				case System.Windows.Input.Key.F5:
					var viewModel = ViewModel;
					if (viewModel != null)
					{
						viewModel.Refresh();
					}
					//var cancellationTokenSource = _cancellationTokenSource;
					//if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
					//cancellationTokenSource = new CancellationTokenSource();
					//_cancellationTokenSource = cancellationTokenSource;
					//((GrouppingLogicPluginBase)DataContext).BeginGenerateGroupsAsync(GenerateGroupsAsyncCompleted, cancellationTokenSource.Token);
					break;
			}
		}

		#endregion
	}
}